package com.woniuxy.products.controller;

import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.commons.entity.Products;
import com.woniuxy.commons.entity.Rate;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.commons.utils.ResultStatus;
import com.woniuxy.products.entity.ProductsAndRate;
import com.woniuxy.products.entity.ProductsAndRateAndRepayment;
import com.woniuxy.products.service.ProductsService;
import com.woniuxy.products.service.RateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author author
 * @create 2021-11-16 15:03
 */
//@CrossOrigin  //解决跨域
@RestController
@RequestMapping("/products")
public class ProductsController {
    @Resource
    private ProductsService productsService;

    @Resource
    private RateService rateService;


//添加产品
    @GetMapping("/add")
    public ResponseResult addProducts(ProductsAndRate productsAndRate){

        System.out.println(productsAndRate);

        Products products = new Products();
        products.setPid(UUID.randomUUID().toString());
        products.setRateid(UUID.randomUUID().toString());
        products.setIntroduce(productsAndRate.getIntroduce());
        products.setPaname(productsAndRate.getPaname());
        products.setPayment(productsAndRate.getPayment());
        products.setEid(productsAndRate.getEid());


        Rate rate = new Rate();
        rate.setRateid(products.getRateid());
        rate.setMonthrate(productsAndRate.getMonthrate());
        rate.setIssue(productsAndRate.getIssue());

        if(productsService.addProducts(products.setPid(UUID.randomUUID().toString()))){

            rateService.addRate(rate);


           ResponseResult result = new ResponseResult();
           result.setStatus(ResultStatus.SELECT_YES);
           result.setMessage("添加成功");
           result.setCode(200);
           return result;
       }
        ResponseResult result = new ResponseResult();
        result.setStatus(ResultStatus.NO_PERMS);
        result.setMessage("添加失败");
        result.setCode(400);
        return result;
    }


    //查询所有产品
    @GetMapping("/selectAllByPage")
    public ResponseResult<PageBean<Products>> selectAll(
    ProductsAndRateAndRepayment productsAndRateAndRepayment, int pageIndex, int pageSize
    ){
        System.out.println(productsAndRateAndRepayment);
        ResponseResult<PageBean<Products>> result = productsService.selectProductsByPage(productsAndRateAndRepayment, pageIndex, pageSize);
        return result;
    }


    //修改表中数据
    @PutMapping("/uptate")
    public String uuotateProducts(@RequestBody Products products){
        System.out.println("111:"+products);
        productsService.updateProductsService(products);
        return "成功";
    }


    //删除表中数据
    @DeleteMapping("/deleteProductos/{pid}")
    public String deleteProductos(@PathVariable String pid){
        productsService.deleteByPidService(pid);
        return "成功删除！";
    }
}
